//
//  LFBAnimationManager.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/10.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBAnimationManager.h"

@interface LFBAnimationManager () <CAAnimationDelegate>

@end

@implementation LFBAnimationManager

- (void)createAnimationWithStartPoint:(CGPoint)startPoint andView:(UIView *)animationView
{
    animationView.center = startPoint;
    
    // 添加imageView 添加到window上
    [kWindow addSubview:animationView];
    
    //创建组动画对象
    CAAnimationGroup *animationGrop = [CAAnimationGroup animation];
    
    //创建基本动画控制大小缩放
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basic.toValue = @(0.1);
    
    //创建关键帧动画
    
    // 计算控制点
    CGFloat controlPointX = (ScreenWidth - startPoint.x) > startPoint.x ? startPoint.x + 200 : startPoint.x - 200;
    CGPoint controlPoint = CGPointMake(controlPointX, startPoint.y - 200);
    
    // 终点
    CGPoint endPoint = CGPointMake(230, kWindow.frame.size.height - 50);
    
    // 实例化路径
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    
    [bezierPath moveToPoint:startPoint];
    [bezierPath addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    
    // 动画对象
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    keyFrame.path = bezierPath.CGPath;
    
    //将动画添加到组动画
    animationGrop.animations = @[basic,keyFrame];
    
    animationGrop.duration = 1;
    
    animationGrop.removedOnCompletion = NO;
    animationGrop.fillMode = kCAFillModeBoth;
    
    animationGrop.delegate = self;
    
    [animationGrop setValue:animationView forKey:@"animationView"];
    
    // 添加动画
    [animationView.layer addAnimation:animationGrop forKey:nil];
}

#pragma mark - 动画结束
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    // 获取传递的imageView
    UIView *animationView = [anim valueForKey:@"animationView"];
    
    [animationView removeFromSuperview];
}
@end
